﻿using System;
using System.Collections.Generic;
using CookComputing.XmlRpc;
//
using PerceptiveMCAPI.Types;
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Methods
{

   public class campaignShareReport : Api_BaseExecute
   {
      private new campaignShareReportInput Input { get; set; }
      private campaignShareReportOutput Output { get; set; }
      // =============================================== constructors
      public campaignShareReport() { }
      // ---------
      public campaignShareReport( campaignShareReportInput input ) { Input = input; }
      // =============================================== Execute
      public campaignShareReportOutput Execute()
      {
         // empty constructor & Execute() is bad
         if ( Input == null )
            throw new ArgumentException( "Input class not provided" );
         // do the deed
         Output = Execute( Input );
         return Output;
      }
      // ---------------------------------------------
      public campaignShareReportOutput Execute( campaignShareReportInput input )
      {
         Input = input;
         Output = new campaignShareReportOutput( Input );
         ValidationIsOK = true;
         //-------------------------
         if ( Input.api_Validate )
         {
            Output.api_ValidatorMessages = ValidateInput.Validate( Input );
            ValidationIsOK = ValidateInput.IsOK( Output.api_ValidatorMessages );
         }
         //------------------------- do it
         if ( ValidationIsOK )
         {
            ApiRun( Input, input.parms.apikey );  // execute API & format output
         }
         // ---
         return Output;
      }

      #region ================================================================= API Calls

      // ---------------------------------------------------------- XmlRpc
      protected override void ExecuteXmlRpc()
      {
         try
         {
            XmlRpcStruct options = FormatOptions( Input.parms.opts );
            x_campaignShareReport rpcResult = apiXmlRpc.api.campaignShareReport( Input.parms.apikey, Input.parms.cid, options );
            Output.Format( rpcResult );
         }
         catch ( XmlRpcFaultException fex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( fex ) );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }
      // -----------------
      private XmlRpcStruct FormatOptions( Dictionary<string, object> opts )
      {
         XmlRpcStruct options = new XmlRpcStruct();
         foreach ( KeyValuePair<string, object> opt in opts )
         {
            if ( opt.Key != "theme" )
            {
               options.Add( opt.Key, opt.Value );
            }
            else
            {
               XmlRpcStruct theme = new XmlRpcStruct();
               Dictionary<string, string> themes = opt.Value as Dictionary<string, string>;
               foreach ( var tc in themes )
               {
                  theme.Add( tc.Key, tc.Value );
               }
               options.Add( "theme", theme );
            }
         }
         //
         return options;
      }

      // ---------------------------------------------------------- Serial Post
      protected override void ExecuteSerialPost()
      {
         try
         {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add( "apikey", Input.parms.apikey );
            dict.Add( "cid", Input.parms.cid );
            foreach ( KeyValuePair<string, object> opt in Input.parms.opts )
            {
               if ( opt.Key.ToLower() != "theme" )
                  dict.Add( string.Format( "opts[{0}]", opt.Key ), opt.Value.ToString() );
               else
               {
                  foreach ( KeyValuePair<string, string> color in opt.Value as Dictionary<string, string> )
                  {
                     dict.Add( string.Format( "opts[theme][{0}]", color.Key ), color.Value );
                  }
               }
            }
            // -- do it
            string api_Request = "";
            string api_Response = apiSerial.ProcessRequest( Input, dict, out api_Request );
            Output.Format( api_Request, api_Response );
         }
         catch ( Exception ex )
         {
            Output.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      #endregion
   }
}